package com.zz.yt.lib.bluetooth.delegate.print.gp;


import android.bluetooth.BluetoothDevice;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.gprinter.io.BluetoothPort;
import com.zz.yt.lib.bluetooth.base.BaseBluetoothDelegate;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * 蓝牙连接
 *
 * @author qf
 * @version 1.0
 **/
public abstract class BaseBluetoothGpDelegate extends BaseBluetoothDelegate {


    protected BluetoothPort bluetoothPort = null;


    /*******************读取数据********************/
    protected void inPermission() {
        Observable
                .create((ObservableOnSubscribe<String>) emitter -> {
                    // 得到蓝牙设备句柄
                    final BluetoothDevice mDevice = mBluetooth.getRemoteDevice(mac);
                    bluetoothPort = new BluetoothPort(mac);
                    isPrint = bluetoothPort.openPort();
                    //连接
                    if (isPrint) {
                        emitter.onNext("连接" + mDevice.getName() + "成功！");
                        emitter.onComplete();
                    } else {
                        emitter.onNext("连接失败！");
                    }
                })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        showLoading();
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        hideLoading();
                        ToastUtils.showShort(s);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        hideLoading();
                        isPrint = false;
                        ToastUtils.showShort("连接失败！");
                    }

                    @Override
                    public void onComplete() {
                        inRx();
                    }
                });
    }


}
